package com.zyq.service.impl;

import com.zyq.mapper.ArticleMapper;
import com.zyq.mapper.GetLikeAndCollectMapper;
import com.zyq.mapper.SearchWordMapper;
import com.zyq.model.entity.Article;
import com.zyq.model.request.ArticleDto;
import com.zyq.model.request.ContentDto;
import com.zyq.model.response.HomePage10Response;
import com.zyq.model.response.HomePageArticle2Response;
import com.zyq.service.IArticleService;
import com.zyq.util.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 文章信息Service业务层处理
 *
 * @author zyq
 * @date 2021-12-02
 */
@Service
public class ArticleServiceImpl implements IArticleService
{
    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private GetLikeAndCollectMapper getLikeAndCollectMapper;

    @Autowired
    private UserUtil userUtil;

    @Autowired
    private SearchWordMapper searchWordMapper;


    /**
     * 查询文章信息
     *
     * @param id 文章信息ID
     * @return 文章信息
     */
    @Override
    public Article selectArticleById(Long id)
    {
        articleMapper.updateArticleById(id);
        Article article = articleMapper.selectArticleById(id);
//        article.setCountCollect(getLikeAndCollectMapper.getCountCollectByArticleId(id));
//        article.setCountGetLike(getLikeAndCollectMapper.getCountGetLikeByArticleId(id));
        return article;
    }

    /**
     * 查询文章信息列表
     *
     * @param article 文章信息
     * @return 文章信息
     */
    @Override
    public List<Article> selectArticleList(ContentDto article)
    {
        if(article.getContent()!=null && article.getContent().trim()!="" ){
            if(searchWordMapper.ifWord(article.getContent())>0){
                searchWordMapper.updateCount(article.getContent());
            }
            else
                searchWordMapper.insertWord(article.getContent());
        }

        return articleMapper.selectArticleList(article);
    }

    @Override
    public List<Article> selectArticleListByAdmin(Article article)
    {
        return articleMapper.selectArticleListByAdmin(article);
    }

    /**
     * 新增文章信息
     *
     * @param article 文章信息
     * @return 结果
     */
    @Override
    public int insertArticle(Article article)
    {
        article.setTime(new Date());
        return articleMapper.insertArticle(article);
    }

    /**
     * 修改文章信息
     *
     * @param article 文章信息
     * @return 结果
     */
    @Override
    public int updateArticle(Article article)
    {
        return articleMapper.updateArticle(article);
    }

    /**
     * 批量删除文章信息
     *
     * @param ids 需要删除的文章信息ID
     * @return 结果
     */
    @Override
    public int deleteArticleByIds(Long[] ids)
    {
        return articleMapper.deleteArticleByIds(ids);
    }

    /**
     * 删除文章信息信息
     *
     * @param id 文章信息ID
     * @return 结果
     */
    @Override
    public int deleteArticleById(Long id)
    {
        return articleMapper.deleteArticleById(id);
    }

    /**
     * 前10文章
     * @return
     */
    @Override
    public List<HomePage10Response> select10(){
        return articleMapper.select10();
    }

    /**
     * 今日热点2篇文章
     * @return
     */
    @Override
    public List<HomePageArticle2Response> select2(){
        return articleMapper.select2();
    }

    /**
     * 查询用户收藏的文章
     */
    @Override
    public List<Article> selectArticleListByCollect(){
        return articleMapper.selectArticleListByCollectUserId(userUtil.getUserId());
    }
    @Override
    public String selectArticleGetLike(Long topicId,Long userId){
        if(getLikeAndCollectMapper.ifGetLike(topicId,userId)>0){
            return "取消点赞";
        }
        else {
            return "点赞";
        }
    }

    @Override
    public String selectArticleCollect(Long topicId,Long userId){
        if(getLikeAndCollectMapper.ifCollect(topicId,userId)>0){
            return "取消收藏";
        }
        else {
            return "收藏";
        }
    }
};
